package dungnt.vpoint.mathtest.db;

//import java.sql.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class ScoreDatabase {
	public static final String SID = "scoreId";
	public static final String SOPERT = "opert";
	public static final String SNAME = "name";
	public static final String SSCORE = "score";
	public static final String STIME = "time";

	public static final String DB_NAME = "Score";
	public static final String DB_TABLE = "Score";
	public static final int DB_VERSION = 1;

	private final Context mContext;
	private SQLiteDatabase mDB;
	private DBHelper mDBHelper;
	private static String ORDER_BY = SSCORE + " DESC";

	/**
     * 
     */
	public ScoreDatabase(Context c) {
		this.mContext = c;
	}

	/**
     * 
     */
	private static class DBHelper extends SQLiteOpenHelper {

		public DBHelper(Context context, String name, CursorFactory factory,
				int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			try {
				db.execSQL("CREATE TABLE Score(scoreId INTEGER PRIMARY KEY autoincrement, opert INTEGER, name TEXT, score INTEGER, time TEXT);");
			} catch (SQLException ex) {
				ex.printStackTrace();
			}
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.i("DBAdapter", "Updating database...");
			db.execSQL("DROP TABLE IF EXISTS Score");
			onCreate(db);
		}
	}

	/**
     * 
     */
	public ScoreDatabase openDB() {
		mDBHelper = new DBHelper(mContext, DB_NAME, null, DB_VERSION);
		mDB = mDBHelper.getWritableDatabase();
		return this;
	}

	/**
	 * close
	 */
	public void closeDB() {
		mDBHelper.close();
	}

	/**
	 * insert record
	 */
	public long insert(int opert, String name, int score, String time) {
		ContentValues cv = new ContentValues();
		cv.put(SOPERT, opert);
		cv.put(SNAME, name);
		cv.put(SSCORE, score);
		cv.put(STIME, time);
		return mDB.insert(DB_TABLE, null, cv);
	}

	/**
     * 
     */
	public boolean edit(int _id, int opert, String name, int score, String time) {
		ContentValues cv = new ContentValues();
		cv.put(SOPERT, opert);
		cv.put(SNAME, name);
		cv.put(SSCORE, score);
		cv.put(STIME, time);
		return mDB.update(DB_TABLE, cv, SID + "=" + _id, null) > 0;
	}

	/**
     * 
     */
	public boolean remove(int _id) {
		return mDB.delete(DB_TABLE, SID + "=" + _id, null) > 0;
	}

	/**
     * 
     */
	public void removeAll() {
		Cursor mCursor = getAllScores();
		while (mCursor.moveToNext()) {
			remove((int) mCursor.getLong(0));
		}
	}

	/**
     * 
     */
	public Cursor getAllScores() {
		return mDB.query(DB_TABLE, new String[] { SID, SOPERT, SNAME, SSCORE,
				STIME}, null, null, null, null, null);
	}

	/**
     * 
     */
	public Cursor getHighScores() {
		return mDB.query(DB_TABLE, new String[] { SID, SOPERT, SNAME, SSCORE,
				STIME}, null, null, null, null, ORDER_BY);
	}
	
	
	public Cursor getHighScoresAdd() {
		return mDB.query(true,DB_TABLE, new String[] { SID, SOPERT, SNAME, SSCORE,
				STIME}, SOPERT + "=" + 0, null, null, null,ORDER_BY, null);
	}
	
	
	public Cursor getHighScoresSubtract() {
		return mDB.query(true,DB_TABLE, new String[] { SID, SOPERT, SNAME, SSCORE,
				STIME}, SOPERT + "=" + 1, null, null, null, ORDER_BY, null);
	}
	
	
	public Cursor getHighScoresMultiply() {
		return mDB.query(true,DB_TABLE, new String[] { SID, SOPERT, SNAME, SSCORE,
				STIME}, SOPERT + "=" + 2, null, null, null, ORDER_BY, null);
	}
	
	
	public Cursor getHighScoresDevide() {
		return mDB.query(true,DB_TABLE, new String[] { SID, SOPERT, SNAME, SSCORE,
				STIME}, SOPERT + "=" + 3, null, null, null, ORDER_BY, null);
	}
	
	
	public Cursor getHighScoresFinal() {
		return mDB.query(true,DB_TABLE, new String[] { SID, SOPERT, SNAME, SSCORE,
				STIME}, SOPERT + "=" + 4, null, null, null, ORDER_BY, null);
	}
	

	/**
     * 
     */
	public Cursor getScoreById(int _id) {
		Cursor mCursor = mDB.query(true, DB_TABLE, new String[] { SID, SOPERT,
				SNAME, SSCORE, STIME}, SID + "=" + _id, null, null,
				null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}

		return mCursor;
	}
}